import { def } from "./utils";

let arrayMethods = Array.prototype;
export const methodsObj = Object.create(arrayMethods);

let methods = ["push", "pop", "shift", "unshift", "splice", "reverse", "sort"];

methods.forEach((methodName) => {
  let oldMthods = arrayMethods[methodName];
  def(methodsObj, methodName, function () {
    let ob = this.__ob__;
    var inserted = null;

    switch (methodName) {
      case "push":
      case "unshift":
        inserted = [...arguments];
        break;
      case "splice":
        inserted = [...arguments].slice(2);
        break;
    }

    if (inserted) {
      ob.observeArray(inserted);
    }

    return oldMthods.apply(this, arguments);
  });
});
